package com.ketai.dao.impl;

import com.ketai.dao.BaseDao;
import com.ketai.dao.OrganizationDao;
import com.ketai.pojo.Organization;

import javax.naming.NamingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 高伟华
 * @Description:
 * @Date:Create In 2022-08-06 11:50
 **/
public class OrganizationDaoImpl extends BaseDao implements OrganizationDao {
    @Override
    public List<Organization> queryOrganizationList() throws SQLException, NamingException {
        List<Organization>organizationList = new ArrayList<>();
        String sql ="select * from organizationbase";
        ResultSet rs =query(sql,null);
        while (rs.next()){
        Organization organization = new Organization();
            organization.setId(rs.getInt("id"));
            organization.setName(rs.getString("name"));
            organization.setBrief(rs.getString("brief"));
            organization.setRespon(rs.getString("respon"));
            organization.setNumber(rs.getString("number"));
            organization.setAddress(rs.getString("address"));
            organization.setCreated(rs.getString("created"));
            organization.setAuditing(rs.getInt("auditing"));
            organization.setImages(rs.getString("images"));
            organizationList.add(organization);
        }
        closeAll(rs);
        return organizationList;
    }

    @Override
    public int deleteOrganizationById(Integer id) throws SQLException, NamingException {
String sql ="delete from organizationbase where id="+id;
        return curdMethod(sql,null);
    }


    @Override
    public int addOrganization(Organization organization) throws SQLException, NamingException {
        String sql= "INSERT INTO `researchtrip`.`organizationbase` ( `name`, `brief`, `Respon`, `number`, `address`, `Created`,`images`') VALUES('?','?','?','?','?','?','?','?')";
        Object [] params ={organization.getName(),organization.getBrief(),organization.getRespon(),organization.getNumber(),organization.getAddress(),organization.getCreated(),organization.getImages()};
        return curdMethod(sql,params);
    }

    @Override
    public Organization organizationById(String id) throws SQLException, NamingException {
        String sql ="select * from organizationbase where id="+id;
        ResultSet rs= query(sql,null);
        Organization organization = new Organization();
        while (rs.next()){
            organization.setId(rs.getInt("id"));
            organization.setName(rs.getString("name"));
            organization.setBrief(rs.getString("brief"));
            organization.setRespon(rs.getString("respon"));
            organization.setNumber(rs.getString("number"));
            organization.setAddress(rs.getString("address"));
            organization.setCreated(rs.getString("created"));
            organization.setAuditing(rs.getInt("auditing"));
            organization.setImages(rs.getString("images"));

        }
        return organization ;
    }

    @Override
    public int checkedOrganization(String sql, Object[] params) throws SQLException, NamingException {
        int result=curdMethod(sql, params);
        return result;
    }


}
